package cn.iocoder.yudao.module.fengdong.smartelderly.service.billing;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.billing.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.billing.BillingDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.fengdong.smartelderly.convert.billing.BillingConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.mysql.billing.BillingMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.fengdong.smartelderly.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 收费记录 Service 实现类
 *
 * @author 沣东养老院管理员
 */
@Service
@Validated
public class BillingServiceImpl implements BillingService {

    @Resource
    private BillingMapper billingMapper;

    @Override
    public Long createBilling(BillingCreateReqVO createReqVO) {
        // 插入
        BillingDO billing = BillingConvert.INSTANCE.convert(createReqVO);
        billingMapper.insert(billing);
        // 返回
        return billing.getId();
    }

    @Override
    public void updateBilling(BillingUpdateReqVO updateReqVO) {
        // 校验存在
        validateBillingExists(updateReqVO.getId());
        // 更新
        BillingDO updateObj = BillingConvert.INSTANCE.convert(updateReqVO);
        billingMapper.updateById(updateObj);
    }

    @Override
    public void deleteBilling(Long id) {
        // 校验存在
        validateBillingExists(id);
        // 删除
        billingMapper.deleteById(id);
    }

    private void validateBillingExists(Long id) {
        if (billingMapper.selectById(id) == null) {
            throw exception(BILLING_NOT_EXISTS);
        }
    }

    @Override
    public BillingDO getBilling(Long id) {
        return billingMapper.selectById(id);
    }

    @Override
    public List<BillingDO> getBillingList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return billingMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BillingDO> getBillingPage(BillingPageReqVO pageReqVO) {
        return billingMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BillingDO> getBillingList(BillingExportReqVO exportReqVO) {
        return billingMapper.selectList(exportReqVO);
    }

}
